$(document).ready(function () {
    var editor = CodeMirror.fromTextArea(document.getElementById('code'), {
        parserfile: ["parsepython.js"],
        autofocus: true,
        theme: "solarized dark",
        //path: "static/env/codemirror/js/",
        lineNumbers: true,
        textWrapping: false,
        indentUnit: 4,
        height: "160px",
        fontSize: "9pt",
        autoMatchParens: true,
        parserConfig: {'pythonVersion': 3, 'strictErrors': true},
        onKeyEvent: function (editor, e) {
            if (e.keyCode === 13 && e.type === "keydown") {
                get_skulpt(e)
            }
        }
    });
    function get_skulpt(e) {
        var output = $('#edoutput');
        var outf = function (text) {
            output.text(output.text() + text);
        };
        Sk.configure({output: outf, read: builtinRead,__future__:Sk.python3,inputfunTakesPrompt: !0});
        Sk.canvas = "mycanvas";
        if (editor.getValue().indexOf('turtle') > -1 ) {
                $('#mycanvas').show();
            output.hide()
            } else{
                $('#mycanvas').hide();
                output.show()
            }
         (Sk.TurtleGraphics || (Sk.TurtleGraphics = {})).target = 'mycanvas';
        Sk.pre = "edoutput";
         var promise=Sk.misceval.asyncToPromise(function(){
             return Sk.importMainWithBody("<stdin>", !1, editor.getValue(),!0);
         });
         promise.then(function () {

         });
        return promise
        //Sk.importMainWithBody("<stdin>", !1, editor.getValue(),!0);
    }
    function builtinRead(x) {
         if (Sk.builtinFiles === undefined || Sk.builtinFiles["files"][x] === undefined)
            throw "File not found: '" + x + "'";
        return Sk.builtinFiles["files"][x];
    }
    editor.focus();
    $('#run_code').click(function () {
        console.log('1');
        get_skulpt(editor);
    });
});
